package cn.itcast.bos.web.action;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.AliMessageUtils;
import cn.itcast.bos.utils.MailUtils;
import cn.itcast.crm.domain.Customer;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer> {
	
	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;

	@Action(value = "customer_sendSms")
	public String sendMsg() {
		// 生成手机验证码
		System.out.println(123456);
		/*final String randomCode = RandomStringUtils.randomNumeric(4);
		// 存到session 后面校验验证码
		ServletActionContext.getRequest().getSession().setAttribute("telephoneMag", randomCode);
		System.out.println("生成的验证码为:" + randomCode);
		
		jmsTemplate.send("bos_msg", new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage = session.createMapMessage();
				mapMessage.setString("telephone", model.getTelephone());
				mapMessage.setString("magCode", randomCode);
				return mapMessage;
			}
		});*/
		
		try {
			// AliMessageUtils.sendSmsByHTTP(model.getTelephone(), randomCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return NONE;
	}

	private String checkcode;

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Action(value = "customerregist", results = {
			@Result(name = "success", location = "signup-success.html", type = "redirect"),
			@Result(name = "input", location = "signup.html", type = "redirect") })
	public String regist() {
		// 先校验验证码
		String sessioncode = (String) ServletActionContext.getRequest().getSession().getAttribute("telephoneMag");
		if (sessioncode == null || !sessioncode.equals(checkcode)) {
			return INPUT;
		}
		// 注册成功 进行保存
		WebClient.create("http://localhost:9002/crm_management/services/customerService/customer")
				.type(MediaType.APPLICATION_JSON).post(model);
		System.out.println("注册成功");
		// 随机生成24位激活码
		String code = RandomStringUtils.randomNumeric(24);
		// 存入redis
		redisTemplate.opsForValue().set(model.getTelephone(), code, 24, TimeUnit.HOURS);
		String content = "尊敬的客户你好,请与24小时进行邮箱绑定,点击下方链接完成绑定:" + "<a href='" + MailUtils.activeUrl + "?telephone="
				+ model.getTelephone() + "&checkcode=" + code + "'>邮件绑定</a>";
		MailUtils.sendMail("邮件绑定", content, model.getEmail());
		return SUCCESS;
	}

	/*
	 * http://localhost:9003/bos_fore/customer_activeMail?telephone=18258952076&
	 * checkcode=721128715323328666568787
	 */
	// 属性驱动
	private String code;

	public void setCode(String code) {
		this.code = code;
	}

	@Action("customer_activeMail")
	public String activeMail() throws Exception {
		
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		// 获得redis里的验证码
		String activeCodeRedis = redisTemplate.opsForValue().get(model.getTelephone());
		// 判断code
		if (activeCodeRedis == null || activeCodeRedis.equals(code)) {// 激活码无效
			ServletActionContext.getResponse().getWriter().println("未能成功绑定邮箱,请重新注册");
		}
		// 激活码有效 调用
		Customer customer = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/customer/telephone/"
						+ model.getTelephone())
				.accept(MediaType.APPLICATION_JSON).get(Customer.class);
		// 判断对象里面type的值
		if (customer.getType() == null ||customer.getType() != 1 ) {
			// 没有绑定 进行绑定
			WebClient.create("http://localhost:9002/crm_management/services/customerService/customer/updatetype/"
					+ model.getTelephone()).put(null);

			ServletActionContext.getResponse().getWriter().println("绑定成功");
		} else {
			// 已经激活
			ServletActionContext.getResponse().getWriter().println("已经激活");
		}
		// 删除激活码对应的key
		redisTemplate.delete(model.getTelephone());

		return NONE;
	}
}
